package com.xdcplus.web.service.wip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.exceptions.XdcWebException;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingRecordDTO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingRecordFilterDTO;
import com.xdcplus.web.common.pojo.entity.wip.WipMarkingRecord;
import com.xdcplus.web.common.pojo.query.wip.WipMarkingRecordQuery;
import com.xdcplus.web.common.pojo.vo.wip.WipMarkingRecordVO;
import com.xdcplus.web.mapper.wip.WipMarkingRecordMapper;
import com.xdcplus.web.service.wip.WipMarkingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

/**
 * 打标结批记录表(按工序维度)(WipMarkingRecord)表服务实现类
 *
 * @author Devin.sang
 * @since 2023-05-15 22:05:44
 */
@Slf4j
@Service("wipMarkingRecordService")
public class WipMarkingRecordServiceImpl extends BaseServiceImpl<WipMarkingRecordMapper, WipMarkingRecord, WipMarkingRecord, WipMarkingRecordVO> implements WipMarkingRecordService {

    @Autowired
    protected WipMarkingRecordMapper wipMarkingRecordMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingRecord(WipMarkingRecordDTO wipMarkingRecordDTO) {

        WipMarkingRecord wipMarkingRecord = new WipMarkingRecord();
        BeanUtil.copyProperties(wipMarkingRecordDTO, wipMarkingRecord);
        wipMarkingRecord.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(wipMarkingRecord);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingRecord(WipMarkingRecordDTO wipMarkingRecordDTO) {

        WipMarkingRecord wipMarkingRecord = this.getById(wipMarkingRecordDTO.getId());
        if (ObjectUtil.isNull(wipMarkingRecord)) {
            log.error("updateWipMarkingRecord() The WipMarkingRecord does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingRecordDTO, wipMarkingRecord);
        wipMarkingRecord.setUpdatedUser(wipMarkingRecordDTO.getUpdatedUser());
        wipMarkingRecord.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingRecord);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingRecord> wipMarkingRecordList) {

        wipMarkingRecordList.forEach(wipMarkingRecord -> {
            WipMarkingRecord wipMarkingRecordParam = new WipMarkingRecord();
            wipMarkingRecordParam.setId(wipMarkingRecord.getId());
            if (ObjectUtil.isNotNull(wipMarkingRecord.getId())) {
                wipMarkingRecord.setId(wipMarkingRecord.getId());
                wipMarkingRecord.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingRecord> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingRecord::getId, wipMarkingRecord.getId());
                update(wipMarkingRecord, lambdaUpdate);
            } else {
                wipMarkingRecord.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingRecord);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingRecordDTO> wipMarkingRecordDTOList) {

        List<WipMarkingRecord> wipMarkingRecordList = BeanUtils.copyProperties(wipMarkingRecordDTOList, WipMarkingRecord.class);
        return saveOrUpdateBatch(wipMarkingRecordList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingRecordLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingRecord wipMarkingRecord = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingRecord)) {
            log.error("deleteWipMarkingRecord() The WipMarkingRecord does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingRecordByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingRecord wipMarkingRecord = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingRecord)) {
				log.error("deleteWipMarkingRecord() The WipMarkingRecord does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingRecord> queryWipMarkingRecordList(WipMarkingRecordFilterDTO wipMarkingRecordFilterDTO) {
        WipMarkingRecordQuery wipMarkingRecordQuery = BeanUtil.copyProperties(wipMarkingRecordFilterDTO, WipMarkingRecordQuery.class);

        return wipMarkingRecordMapper.queryWipMarkingRecord(wipMarkingRecordQuery);
    }

    @Override
    public List<WipMarkingRecordVO> queryWipMarkingRecordVOList(WipMarkingRecordFilterDTO wipMarkingRecordFilterDTO) {
        return this.objectConversion(queryWipMarkingRecordList(wipMarkingRecordFilterDTO));
    }

    @Override
    public PageVO<WipMarkingRecordVO> queryWipMarkingRecord(WipMarkingRecordFilterDTO wipMarkingRecordFilterDTO) {
        PageVO<WipMarkingRecordVO> pageVO = new PageVO<>();

        if (wipMarkingRecordFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingRecordFilterDTO.getCurrentPage(), wipMarkingRecordFilterDTO.getPageSize(),
                    wipMarkingRecordFilterDTO.getOrderType(), wipMarkingRecordFilterDTO.getOrderField());
        }

        List<WipMarkingRecord> wipMarkingRecordList = queryWipMarkingRecordList(wipMarkingRecordFilterDTO);

        PageInfo<WipMarkingRecord> pageInfo = new PageInfo<>(wipMarkingRecordList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingRecordList));

        return pageVO;
    }

    @Override
    public WipMarkingRecordVO queryWipMarkingRecordById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
